/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2022-2022. All rights reserved.
 */

import { expect } from '@jest/globals';

import { initByStageEmptyApp, initExecuteCommand, runHvigor } from '../common/common';
import { ModuleBuildProfileModifier } from '../common/modifier/module-build-profile-modifier';
import { ModuleJsonModifier } from '../common/modifier/module-json-modifier';
import { PackageJsonModifier } from '../common/modifier/package-json-modifier';
import { ProjectBuildProfileModifier } from '../common/modifier/project-build-profile-modifier';

describe('Adapt Test', () => {
  test.concurrent('Stage with runtimeOS HarmonyOs library', async () => {
    await initByStageEmptyApp({
      prepare: async (dir) => {
        const entryPackageJson = new PackageJsonModifier(dir, 'entry');
        entryPackageJson.updateDependency('library', 'file:../library');
        entryPackageJson.outputSync();
      },
      cb: async (dir) => {
        const moduleBuildProfileModifier = new ModuleBuildProfileModifier(dir, 'entry');
        moduleBuildProfileModifier.updateTarget({
          name: 'default',
          runtimeOS: 'HarmonyOS'
        }).outputSync();
        const moduleJsonModifier = new ModuleJsonModifier(dir, 'entry');
        moduleJsonModifier.setExtensionAbilities([]).outputSync();

        const libraryModuleBuildProfileModifier = new ModuleBuildProfileModifier(dir, 'library');
        libraryModuleBuildProfileModifier.updateTarget({
          name: 'default',
          runtimeOS: 'HarmonyOS'
        }).outputSync();

        const projectBuildProfileModifier = new ProjectBuildProfileModifier(dir);

        // 修改deviceType default为phone 校验通过 打包成功
        projectBuildProfileModifier.updateProduct({
          'name': 'default',
          'signingConfig': 'hos',
        }).outputSync();

        const commands = initExecuteCommand({
          mode: 'module',
          product: 'default',
          module: 'entry@default',
          tasks: 'assembleHap',
        });
        const result = runHvigor(dir, commands);

        // run successful
        expect(result.stdout.includes('BUILD SUCCESSFUL')).toBeTruthy();
      },
      runNpmInstall: true
    });
  });
});
